/*
 * HW1.asm
 *
 *  Created: 2/18/2014 11:26:55 AM
 *   Author: Omar Salazar
 */ 
	
	 LDI	R16, HIGH(RAMEND)
	 OUT	SPH, R16
	 LDI	R16, LOW(RAMEND)
	 OUT	SPL, R16
;************************************************************************************
;The segment below is to add the 20 numbers onto the stack.
	 LDI	R17, 35			;start at 35
	 LDI	R19, 5			;constant for 5
	 LDI	R25, 3			;constant for 3
	 LDI	R18, 20			;loop to control the amount of 20 numbers
 PLUS5:
	 DEC	R18				;decrement the loop control by 1
	 ADD	R17, R19		;add 5 to R17
	 PUSH	R17				;Push the result onto the stack
 MINUS3:
	 DEC	R18				;decrement loop control by 1
	 SUB	R17, R25		;subtract 3 from R17
	 PUSH	R17				;push the result onto the stack
	 CPI	R18, 0			;Compare if the loop control is 0
	 BREQ	NEXT			;If it is zero, jump to NEXT
	 RJMP	PLUS5
;************************************************************************************
 NEXT:
	 IN		R26, SPL		;Load the low part of the stack
	 IN		R27, SPH		;Load the high part of the stack.
;************************************************************************************
;This segment of code will read in the values on the stack and check each one if it is
;divisible by 3, then by 5.  The method it will do is by subtracting and checking if
;the final result is 0, if it is, then a running sum is recorded.
 MAIN:						;R26:R27 is the X register
	 INC	R26				;Increment the stackpointer address
	 LD		R28, X			;Load the value on the stack. X corresponds to R26:R27
	 MOV	R30, R28		;Get a copy of what was on the stack
	 CPI	R30, 0			;Compare R30 to assure that there are no more values
	 BREQ	DONE			;Jump to done since there are no more values on the stack
 CHECK3:  
	 SUBI   R28, 3			;Subtract by 3
	 CPI	R28, 0			;Check if the subtraction resulted in 0
	 BREQ	DIV3			;If it did, then it is divisible by 3
	 BRLT	NOT3			;If less than 0, jump to check if divisible by 5
	 RJMP   CHECK3			;go back to keep subtracting by 3
 DIV3:
	 ADD	R21, R30		;Add the number to the running sum of numbers divisible by 3
	 BRCS	XS				;If a carry occurs, branch to excess to add the bit to the High side
	 RJMP	NOT3			;Now jump to check if divisible by 5
 XS:
	 INC	R20				;This adds the carry bit to the High side of the divisible by 3 running sum
 NOT3:
	 MOV	R28, R30		;Copy the number to check if divisible by 5
 CHECK5:
	 SUBI	R28, 5			;Subtract by 5
	 CPI	R28, 0			;Check if the subtraction resulted in 0
	 BREQ	DIV5			;If it did, then it is divisible by 5
	 BRLT	NOT5			;If less than 0, jump to NOT5 to go to MAIN
	 RJMP	CHECK5			;go back to keep subtracting by 5
  DIV5:
	 ADD	R24, R30		;add the number to the running sum of numbers divisible by 5
	 BRCS	XS2				;If a carry occurs, branch to excess2 to add the bit to the High side
	 RJMP	NOT5			;Jump to NOT5 to go back to MAIN to get the next number.
  XS2:
     INC	R23				;This adds the carry bit to the High side of the divisible by 5 running sum
  NOT5:
	 RJMP	MAIN			;Jump to MAIN to read in the next number on the stack
;************************************************************************************
  DONE:						;FINITO! The running sum for numbers divisible by 3 will be stored in R20:R21 = 0x117
							;The running sum for numbers divisible by 5 will be stored in R23:R24 = 0xB5